package com.liumuxian.exam.service.impl;

import com.liumuxian.exam.dao.SubjectAssoetedDao;
import com.liumuxian.exam.dao.SubjectAssoetedExamClassifyExamInfoDao;
import com.liumuxian.exam.pojo.SubjectAssoeted;
import com.liumuxian.exam.pojo.SubjectAssoetedExamClassifyExamInfo;
import com.liumuxian.exam.service.SubjectAssoetedService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

@Service
@Transactional(readOnly = true)
public class SubjectAssoetedServiceImpl implements SubjectAssoetedService {
    @Autowired
    private SubjectAssoetedDao subjectAssoetedDao;
    @Autowired
    private SubjectAssoetedExamClassifyExamInfoDao subjectAssoetedExamClassifyExamInfoDao;
    @Autowired
    private IdWorker idWorker;
    @Override
    @Transactional
    public void save(SubjectAssoeted subjectAssoeted){
        subjectAssoeted.setId(idWorker.nextId()+"");
        subjectAssoeted.setStatus(1);
        subjectAssoeted.setCreatdate(new Date());
        subjectAssoetedDao.save(subjectAssoeted);
    }
    @Override
    @Transactional
    public void saveAll(List<SubjectAssoeted> subjectAssoeted){
        subjectAssoeted.forEach(s ->{
            s.setId(idWorker.nextId()+"");
            s.setStatus(1);
            s.setCreatdate(new Date());
        });
        subjectAssoetedDao.saveAll(subjectAssoeted);

    }
    @Override
    public List<SubjectAssoeted> findAllByTemplatesetid(String templatesetId){
        return subjectAssoetedDao.findAllByTemplatesetid(templatesetId);
    }

    @Transactional
    public void update(SubjectAssoeted subjectAssoeted){
        SubjectAssoeted subjectAssoeted1 = subjectAssoetedDao.findById(subjectAssoeted.getId()).get();
        BeanUtils.copyProperties(subjectAssoeted,subjectAssoeted1);
        subjectAssoeted1.setUpdatedate(new Date());
        subjectAssoetedDao.saveAndFlush(subjectAssoeted);
    }
    @Transactional
    public void delete(String id){
        SubjectAssoeted subjectAssoeted = subjectAssoetedDao.findById(id).get();
        subjectAssoeted.setUpdatedate(new Date());
        subjectAssoeted.setStatus(0);
        subjectAssoetedDao.saveAndFlush(subjectAssoeted);
    }
    public SubjectAssoeted findById(String id){
        return subjectAssoetedDao.findById(id).get();
    }

    public List<SubjectAssoeted> findAllGesTate(String id,String name,Integer state) {
        return subjectAssoetedDao.findAll((Root<SubjectAssoeted> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<Predicate>();
            if (id != null && !StringUtils.isEmpty(id)) {
                predicateList.add(criteriaBuilder.equal(root.get("id"), id));
            }
            if (name != null && !StringUtils.isEmpty(name)) {
                predicateList.add(criteriaBuilder.like(root.get("name"), "%" + name + "%"));
            }
            if (state != null && !StringUtils.isEmpty(state)) {
                predicateList.add(criteriaBuilder.ge(root.get("state"), (Number) state));
            }
            return criteriaBuilder.and( predicateList.toArray(new Predicate[predicateList.size()]));
        });
    }

}
